//
//  MineViewController.m
//  VmiSJ
//
//  Created by 汪的一声就哭了！ on 2016/12/30.
//  Copyright © 2016年 汪的一声就哭了！. All rights reserved.
//

#import "MineViewController.h"

@interface MineViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) MineHeaderView *headerView;
@end

@implementation MineViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self obtainUserData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"我的";
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-TabHeight) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"MineTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell1"];
     [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell2"];
    self.tableView.backgroundColor = [UIColor clearColor];
    
    self.headerView = [[NSBundle mainBundle] loadNibNamed:@"MineHeaderView" owner:self options:nil].firstObject;
    self.tableView.tableHeaderView = self.headerView;
    self.tableView.tableHeaderView.frame = CGRectMake(0, 0, WIDTH, 180);
    UITapGestureRecognizer *wszlTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(wszlAction:)];
    wszlTap.cancelsTouchesInView = NO;
    [self.tableView.tableHeaderView addGestureRecognizer:wszlTap];
    
    UITapGestureRecognizer *wdqbTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(wdqbAction:)];
    [self.headerView.wdqbView addGestureRecognizer:wdqbTap];
    
    UITapGestureRecognizer *zhmxTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(zhmxAction:)];
    [self.headerView.zhmxView addGestureRecognizer:zhmxTap];
    
    UITapGestureRecognizer *wytx = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(wytxAction:)];
    [self.headerView.wytxView addGestureRecognizer:wytx];
    
}
#pragma mark ----------- 我的钱包
- (void)wdqbAction:(UITapGestureRecognizer *)tap
{
    WDQBViewController *viewC = [[WDQBViewController alloc] init];
    [viewC setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:viewC animated:YES];
}
#pragma mark ----------- 账户明细
- (void)zhmxAction:(UITapGestureRecognizer *)tap
{
    ZHMXViewController *viewC = [[ZHMXViewController alloc] init];
    [viewC setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:viewC animated:YES];
}
#pragma mark ----------- 我要提现
- (void)wytxAction:(UITapGestureRecognizer *)tap
{
    WYTXViewController *viewC = [[WYTXViewController alloc] init];
    [viewC setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:viewC animated:YES];
}
#pragma mark ----------- 完善资料页
- (void)wszlAction:(UITapGestureRecognizer *)tap
{
    if ([tap locationInView:self.tableView.tableHeaderView].x < WIDTH-118 && [tap locationInView:self.tableView.tableHeaderView].y > 55 && [tap locationInView:self.tableView.tableHeaderView].y < 166) {
        WSZLViewController *viewC = [[WSZLViewController alloc] init];
        [viewC setHidesBottomBarWhenPushed:YES];
        [self.navigationController pushViewController:viewC animated:YES];
    }
}
#pragma mark --------- tableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 2;
    }
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section < 2) {
        MineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];
        if (indexPath.section == 0 && indexPath.row == 0) {
            cell.headImageView.image = [UIImage imageNamed:@"我的_V米客服"];
            cell.nameLabel.text = @"V 米客服";
            cell.phoneLabel.text = @"1835956088";
            cell.phoneLabel.hidden = NO;
        }
        else if (indexPath.row == 1) {
            cell.headImageView.image = [UIImage imageNamed:@"我的_关于我们"];
            cell.nameLabel.text = @"关于我们";
            cell.phoneLabel.hidden = YES;
        }
        else if (indexPath.section == 1) {
            cell.headImageView.image = [UIImage imageNamed:@"我的_我的草稿"];
            cell.nameLabel.text = @"我的草稿";
            cell.phoneLabel.hidden = YES;
        }
        return cell;
    } else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
        cell.textLabel.text = @"退出登录";
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.textColor = Color;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && indexPath.row == 0) {
        return 50;
    }
    else if (indexPath.row == 1) {
        return 60;
    }
    else if (indexPath.section == 1) {
        return 70;
    }
    else {
        return 49;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //客服
    if (indexPath.section == 0 && indexPath.row == 0)
    {
        NSString  *qqNumber=@"1835956088";
        
        if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"mqq://"]]) {
            UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];
            NSURL * url=[NSURL URLWithString:[NSString stringWithFormat:@"mqq://im/chat?chat_type=wpa&uin=%@&version=1&src_type=web",qqNumber]];
            
            NSURLRequest *request = [NSURLRequest requestWithURL:url];
            [webView loadRequest:request];
            [self.view addSubview:webView];
        }else{
            
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示信息" message:@"对不起，您还没安装QQ" preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                
            }];
            
            [alertController addAction:cancelAction];
            [self presentViewController:alertController animated:YES completion:nil];
        }
    }
    if (indexPath.section == 1) {
        WDCGViewController *viewC = [[WDCGViewController alloc] init];
        [viewC setHidesBottomBarWhenPushed:YES];
        [self.navigationController pushViewController:viewC animated:YES];
    }
    if (indexPath.section == 2) {
        [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"U_id"];
        AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
        [app setLoginRoot];
    }
}
#pragma mark -------------- 用户信息网络请求
- (void)obtainUserData
{
    [NetRequiest handleDataWithString:[NSString stringWithFormat:@"%@",HTTPLOAD] dictionary:@{@"v1":@"userinfo",@"uid":UID} HUD:YES block:^(id responseObject) {
        if ([responseObject[@"code"] isEqual:@200]) {
            [self.headerView.headImageView sd_setImageWithURL:[NSURL URLWithString:responseObject[@"result"][@"img_url"]] placeholderImage:[UIImage imageNamed:@"占位图"]];
            self.headerView.titleLabel.text = responseObject[@"result"][@"company_name"];
        } else {
            [[SQShowMessage shareMessage] showMessageWithStr:responseObject[@"msg"]];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
